package com.ruoyi.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.MajorConstant;
import com.ruoyi.common.core.domain.entity.SysMajor;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysMajorMapper;
import com.ruoyi.system.service.ISysMajorService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专业管理Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-29
 */
@Service
public class SysMajorServiceImpl extends ServiceImpl<SysMajorMapper, SysMajor> implements ISysMajorService {
    @Autowired
    private ISysUserService userService;

    @Override
    public List<SysMajor> queryList(SysMajor sysMajor) {
        LambdaQueryWrapper<SysMajor> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sysMajor.getName())) {
            lqw.like(SysMajor::getName, sysMajor.getName());
        }
        if (StringUtils.isNotBlank(sysMajor.getCode())) {
            lqw.eq(SysMajor::getCode, sysMajor.getCode());
        }
        if (sysMajor.getStatus() != null) {
            lqw.eq(SysMajor::getStatus, sysMajor.getStatus());
        }
        lqw.orderByAsc(SysMajor::getSortNum);
        return this.list(lqw);
    }

    @Override
    public SysMajor queryByCode(String majorCode) {
        LambdaQueryWrapper<SysMajor> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysMajor::getCode, majorCode);
        return this.getOne(lqw);
    }

    @Override
    public List<SysMajor> listUserEnabled() {
        return listUserEnabled(false);
    }

    @Override
    public String getFilterMajorCode(String majorCode) {
        String userMajorCode = userService.selectUserByUserName(SecurityUtils.getUsername()).getMajorCode();
        //过滤的专业
        String filterMajor = "";
        if (StrUtil.isNotEmpty(majorCode) && !majorCode.equals(MajorConstant.ALL_MAJOR_CODE)) {
            //业务专业非空且不包含全专业，则使用业务专业
            filterMajor = majorCode;
        } else if (userMajorCode.contains(MajorConstant.ALL_MAJOR_CODE)) {
            //业务和用户都是全专业，不过滤
            filterMajor = "";
        } else {
            //业务全专业，用户非全专业，使用用户专业过滤
            filterMajor = userMajorCode;
        }
        return filterMajor;
    }

    @Override
    public List<SysMajor> listUserEnabled(boolean listDisabled) {
        String userMajorCode = userService.selectUserByUserName(SecurityUtils.getUsername()).getMajorCode();
        List<SysMajor> list = new ArrayList<>();
        if (StringUtils.isNotBlank(userMajorCode)) {
            //全专业则返回所有
            if (userMajorCode.contains(MajorConstant.ALL_MAJOR_CODE)) {
                SysMajor param = new SysMajor();
                if (!listDisabled) {
                    param.setStatus("0");
                }
                return queryList(param);
            }
            //否则返回当前专业
            for (String mCode : userMajorCode.split(",")) {
                SysMajor mu = queryByCode(mCode);
                list.add(mu);
            }
        }
        return list;
    }
}
